package com.quectel.core.module.village.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.quectel.constant.global.SqlConstants;
import com.quectel.constant.global.SystemConstants;
import com.quectel.core.module.village.dao.VillageDao;
import com.quectel.core.module.village.dto.VillageDto;
import com.quectel.core.module.village.entity.VillageEntity;
import com.quectel.core.module.village.service.VillageService;
import com.quectel.util.common.CopyUtils;
import com.quectel.util.kit.CacheKit;
import com.quectel.util.kit.Snowflake;
import org.apache.dubbo.config.annotation.DubboService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;

import static com.quectel.core.constants.RedisCacheConstants.PROJECT_NAME;

/**
 * @author caolu
 * @email louis.cao@quectel.com
 * @date 2021-10-18 14:41:21
 */
@DubboService
public class VillageServiceImpl implements VillageService {

    private static final Logger LOGGER = LoggerFactory.getLogger(VillageServiceImpl.class);

    public static final String VILLAGE_CACHE_BY_ID = PROJECT_NAME + "VILLAGE_CACHE_BY_ID_";
    public static final String VILLAGE_CACHE_BY_NAME = PROJECT_NAME + "VILLAGE_CACHE_BY_NAME_";


    @Autowired
    private VillageDao villageDao;


    @Override
    public VillageDto selectCacheByName(Long tenantId, String name) {
        return CacheKit.cacheToRedis(
                () -> selectByName(tenantId, name),
                VILLAGE_CACHE_BY_NAME + tenantId + "_" + name,

                SystemConstants.NOT_NULL_CACHE_EXPIRE_SECONDS
        );
    }

    @Override
    public VillageDto selectByName(Long tenantId, String name) {
        VillageEntity entity = villageDao.selectOne(
                new LambdaQueryWrapper<VillageEntity>()
                        .eq(VillageEntity::getTenantId, tenantId)
                        .eq(VillageEntity::getName, name)
                        .last(SqlConstants.MYSQL_LIMIT_ONE)
        );

        return CopyUtils.copyObj(entity, VillageDto.class);
    }

    @Override
    public VillageDto selectByCode(String code) {
        VillageEntity entity = villageDao.selectOne(
                new LambdaQueryWrapper<VillageEntity>()
                        .eq(VillageEntity::getCode, code)
                        .last(SqlConstants.MYSQL_LIMIT_ONE)
        );

        return CopyUtils.copyObj(entity, VillageDto.class);
    }

    @Override
    public VillageDto selectCacheById(Long id) {
        return CacheKit.cacheToRedis(
                () -> selectById(id),
                VILLAGE_CACHE_BY_ID + id,

                SystemConstants.NOT_NULL_CACHE_EXPIRE_SECONDS
        );
    }

    private void clearIdCache(Long id) {
        CacheKit.invalidRedisCache(VILLAGE_CACHE_BY_ID + id);
    }


    @Override
    public VillageDto selectById(Long id) {
        VillageEntity entity = villageDao.selectById(id);
        return CopyUtils.copyObj(entity, VillageDto.class);
    }

    @Override
    public List<VillageDto> queryList(Map<String, Object> params) {
        List<VillageEntity> list = villageDao.queryList(params);
        return CopyUtils.copyList(list, VillageDto.class);
    }

    @Override
    public int queryTotal(Map<String, Object> params) {
        return villageDao.queryTotal(params);
    }

    @Override
    public Long save(VillageDto dto) {
        VillageEntity entity = CopyUtils.copyObj(dto, VillageEntity.class);
        entity.setId(Snowflake.nextId());
        villageDao.insert(entity);
        return entity.getId();
    }

    @Override
    public void updateById(VillageDto dto) {
        VillageEntity entity = CopyUtils.copyObj(dto, VillageEntity.class);
        villageDao.updateById(entity);

        clearIdCache(dto.getId());
    }


    @Override
    public void deleteById(Long id) {
        villageDao.deleteById(id);
        clearIdCache(id);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteBatch(Long[] ids) {
        for (Long id : ids) {
            deleteById(id);
        }
    }
}
